package com.zhx.mybatislog.interceptor.sql;

import com.zhx.mybatislog.interceptor.utils.SqlUtils;
import com.zhx.mybatislog.user.dao.SysUserMapper;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Properties;

/**
 * 
 * @Description 完整SQL，包括参数替换
 * @author zhx
 *
 */
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class })
//		,@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,RowBounds.class, ResultHandler.class })
})
public class CompleteSQLInterceptor implements Interceptor {

	protected final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	SysUserMapper sysUserMapper;
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.ibatis.plugin.Interceptor#intercept(org.apache.ibatis.plugin.
	 * Invocation)
	 */
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		// 获取xml中的一个select/update/insert/delete节点，是一条SQL语句
		MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
//		Object parameter = null;
		// 获取参数，if语句成立，表示sql语句有参数，参数格式是map形式
//		if (invocation.getArgs().length > 1) {
//			parameter = invocation.getArgs()[1];
//			Field[] declaredFields = parameter.getClass().getDeclaredFields();
//			for(Field field:declaredFields){
//				System.out.println(field.getName());
//			}
//			log.info("parameter = " + parameter);
//		}
		String sqlId = mappedStatement.getId(); // 获取到节点的id,即sql语句的id
		BoundSql boundSql = mappedStatement.getBoundSql(invocation.getArgs()[1]); // BoundSql就是封装myBatis最终产生的sql类
		Object obj = boundSql.getParameterObject();
		String sql = boundSql.getSql();  //预编译sql
		Configuration configuration = mappedStatement.getConfiguration(); // 获取节点的配置
//		String sql = SqlUtils.getCompleteSQL(configuration, boundSql);//完整sql
		if (sql.trim().toUpperCase().startsWith("UPDATE")) {

		}
		log.info(sqlId + "==> : " + sql);
//		userMapper.seleteByid(18006);
		// 执行完上面的任务后，不改变原有的sql执行过程
		return invocation.proceed();
	}

	/**
	 * 
	 * @Description 封装了一下sql语句，使得结果返回完整xml路径下的sql语句节点id + sql语句
	 * @author zhx
	 * @return String
	 * @param configuration
	 * @param boundSql
	 * @param sqlId
	 * @return
	 */
	public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) {
		String sql = SqlUtils.getCompleteSQL(configuration, boundSql);
		StringBuilder str = new StringBuilder(100);
		str.append(sqlId);
		str.append(":");
		str.append(sql);
		return str.toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.ibatis.plugin.Interceptor#plugin(java.lang.Object)
	 */
	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.ibatis.plugin.Interceptor#setProperties(java.util.Properties)
	 */
	@Override
	public void setProperties(Properties properties) {

	}

}